package com.example.module.goods.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.unit.ConvertUnit;
import com.example.common.unit.GetUserInfoBySessionUnit;
import com.example.module.goods.DO.*;
import com.example.module.goods.form.GetGoodsForm;
import com.example.module.goods.form.GoodsAddForm;
import com.example.module.goods.form.GoodsDeleteByIdForm;
import com.example.module.goods.form.GoodsUpdateForm;
import com.example.module.goods.mapper.GoodsMapper;
import com.example.module.goods.service.GoodsService;
import com.example.module.goods.vo.GetGoodsVO;
import com.example.module.user.DO.GetUserByIdDO;
import com.example.module.user.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author ghy
 * @date 2025/08/12
 * @description 商品实现类
 */
@Service
public class GoodsImpl implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private GetUserInfoBySessionUnit getUserInfoBySessionUnit;

    /*
     * 新增商品
     *
     * @param goodsAddForm
     * @return void
     * */
    @Override
    public void addGoods(GoodsAddForm goodsAddForm) {
        //判断用户是否存在（根据用户Id判断）
        Long userId = getUserInfoBySessionUnit.getUserId();
        if (userId == null) {
            throw new RuntimeException("商品新增失败：用户不存在");
        }
        //判断商品类型是否存在（根据商品类型Id判断）
        Long goodsTypeId = goodsAddForm.getGoodsTypeId();
        GetGoodsTypeByGoodsTypeIdDO goodsType = goodsMapper.getGoodsTypeByGoodsTypeId(goodsTypeId);
        if (goodsType == null) {
            throw new RuntimeException("商品新增失败：商品类型不存在");
        }

        GoodsAddDO goodsAddDO = ConvertUnit.FormToDO(goodsAddForm, com.example.module.goods.DO.GoodsAddDO.class);
        goodsAddDO.setUserId(userId);
        goodsMapper.addGoods(goodsAddDO);
    }

    /*
     * 删除商品
     *
     * @param goodsDeleteByIdForm
     * @return void
     * */
    @Override
    public void deleteGoodsById(GoodsDeleteByIdForm goodsDeleteByIdForm) {
        //判断商品是否存在（根据商品Id判断）
        Long goodsId = goodsDeleteByIdForm.getGoodsId();
        //创建GetGoodsForm存goodsId并查询Id对应商品
        GetGoodsForm getGoodsForm = new GetGoodsForm();
        getGoodsForm.setGoodsId(goodsId);
        List<GetGoodsVO> goods = goodsMapper.getGoods(getGoodsForm);
        if (goods.size() == 0) {
            throw new RuntimeException("商品删除失败：商品不存在");
        }
        goodsMapper.deleteGoodsById(goodsDeleteByIdForm);
    }

    /*
     * 更新商品
     *
     * @param goodsUpdateForm
     * @return void
     * */
    @Override
    public void updateGoods(GoodsUpdateForm goodsUpdateForm) {
        //判断商品是否存在（根据商品Id判断）
        Long goodsId = goodsUpdateForm.getGoodsId();
        //创建GetGoodsForm存goodsId并查询Id对应商品
        GetGoodsForm getGoodsForm = new GetGoodsForm();
        getGoodsForm.setGoodsId(goodsId);
        List<GetGoodsVO> goods = goodsMapper.getGoods(getGoodsForm);
        if (goods.size() == 0) {
            throw new RuntimeException("商品修改失败：商品不存在");
        }
        //判断用户是否存在（根据用户Id判断）
        if (goodsUpdateForm.getUserId() != null) {
            Long userId = goodsUpdateForm.getUserId();
            GetUserByIdDO user = userMapper.getUserById(userId);
            if (user == null) {
                throw new RuntimeException("商品修改失败：用户不存在");
            }
        }
        //判断商品类型是否存在（根据商品类型Id判断）
        if (goodsUpdateForm.getGoodsTypeId() != null) {
            Long goodsTypeId = goodsUpdateForm.getGoodsTypeId();
            GetGoodsTypeByGoodsTypeIdDO goodsType = goodsMapper.getGoodsTypeByGoodsTypeId(goodsTypeId);
            if (goodsType == null) {
                throw new RuntimeException("商品修改失败：商品类型不存在");
            }
        }
        //设置商品上架时间为最新时间
        goodsUpdateForm.setGoodsUptime(new Date());
        goodsMapper.updateGoods(goodsUpdateForm);
    }

    /*
     * 查询商品
     *
     * @param getGoodsForm
     * @return List<GetGoodsVO>
     * */
    @Override
    public List<GetGoodsVO> getGoods(GetGoodsForm getGoodsForm) {
        List<GetGoodsVO> getGoodsVOList = goodsMapper.getGoods(getGoodsForm);
        return getGoodsVOList;
    }

    /*
     * 分页查询商品
     *
     * @param getGoodsForm
     * @return Page<GetGoodsVO>
     * */
    @Override
    public Page<GetGoodsVO> getGoodsByPage(GetGoodsForm getGoodsForm) {
        //设置分页参数
        Long current = getGoodsForm.getCurrent() != null ? getGoodsForm.getCurrent() : 1L;
        Long size = getGoodsForm.getSize() != null ? getGoodsForm.getSize() : 10L;
        //构造分页对象
        Page<GetGoodsVO> page = new Page<>(current, size);
        //执行分页查询
        Page<GetGoodsVO> result = goodsMapper.getGoodsByPage(page, getGoodsForm);
        return result;
    }


}
